<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" label-width="220px" :model="formData" :rules="rules">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="formData.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="formData.workNumber" size="mini" class="inputW" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="formData.mobile"
                  size="mini"
                  class="inputW"
                  :disabled="!!$route.params.id"
                />
                <!-- 双取反转布尔值，当id存在就禁用手机，不存在就不禁用 -->
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <select-tree v-model="formData.departmentId" />  <!-- 数据进行父传子，子传父 -->
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="formData.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1">正式</el-option>
                  <el-option label="非正式" :value="2">非正式</el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="formData.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="formData.correctionTime"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
                <!--
                    value-format="yyyy-MM-dd"  规定日期的格式
                 -->
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <image-upload v-model="formData.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="onsubmit">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { addEmployeeAPI, editEmployeeAPI, getEmployeeDetailAPI } from '@/api/employee'
import SelectTree from '@/views/employee/components/select-tree.vue'
import ImageUpload from './components/ImageUpload.vue'
export default {
  name: 'EmployeeDetail',
  components: { SelectTree, ImageUpload },
  data() {
    return {
      formData: {
        username: '', // 员工姓名
        mobile: '', // 手机号
        formOfEmployment: null, // 聘用形式
        workNumber: '', // 员工工号
        departmentId: null, // 员工部门id
        timeOfEntry: '', // 入职日期
        correctionTime: '', // 转正日期
        staffPhoto: '' // 头像
      },
      rules: {
        username: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '输入1-4位', trigger: 'change' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请输入入职日期', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请输入转正日期', trigger: 'blur' },
          { validator: (_, value, callback) => {
            if (!this.formData.timeOfEntry) return callback()
            if (new Date(this.formData.timeOfEntry) > new Date(value)) {
              callback(new Error('转正时间不能小于入职时间'))
            } else {
              callback()
            }
          }, trigger: 'change' }
        ]
      }
    }
  },
  async created() {
    if (!this.$route.params.id) return
    // this.$route.params.id动态路由跳转传递的数据
    const { data } = await getEmployeeDetailAPI(this.$route.params.id)
    this.formData = data
  },
  methods: {
    async onsubmit() {
      await this.$refs.userForm.validate()
      if (this.$route.params.id) {
        await editEmployeeAPI(this.formData)
        this.$message.success('修改成功')
        this.$router.push('/employee')
      } else {
        await addEmployeeAPI(this.formData)
        this.$message.success('新增成功')
        this.$router.push('/employee')
      }
    }
  }
}
</script>

  <style scoped lang="scss">
      .edit-form {
        background: #fff;
        padding: 20px;
        .inputW {
          width: 380px
        }
      }

  </style>
